<?php

namespace application\controllers;

use application\BaseController;
use fastphp\facade\Db;
use fastphp\facade\Request;
use fastphp\facade\Session;
use fastphp\util\Directory;
use fastphp\util\Header;
use fastphp\util\Utils;

class AdminIndexController extends BaseController
{
    public function index()
    {
        if (Request::isGet()) {
            if (!is_file(ROOT . 'token/installed.txt')) {
                Header::setLocation('../../install.php');
                exit();
            }
            $param = Request::get();
            if (!empty($param)) {
                if ($param['flag'] == 0) {
                    if (Session::get('remenber_login')) {
                        Session::clear('remenber_login');
                    }
                    Header::setLocation('user/login');
                    exit;
                }
            }
            if (empty(Session::get('remenber_login'))) {
                Header::setLocation('user/login');
                exit;
            }
            $uid = Session::get('remenber_login');
            $res = Db::table('admin_user')->where('id', $uid)
                ->field('account,avater_img')
                ->find();
            if (empty($res)) {
                Header::setLocation('user/login');
                exit;
            }

            $where = [
                ['status', '=', 1],
            ];
            $menu = Db::table('admin_menus')->where($where)->select();

            $menus = [];
            foreach ($menu as $i => $v) {
                $menus[$v['id']] = $v;
            }

            $tree = [];
            foreach ($menus as $v) {
                if ($v['parent_id'] == 0) {
                    $tree[$v['id']] = $v;
                } else {
                    $tree[$v['parent_id']]['children'][] = $v;
                }
            }

//            $this->json('ok', $tree);
            $this->_view->set("tree", $tree);
            $this->_view->set("userInfo", $res);
            $this->_view->render();
        }
    }

    public function welcome()
    {
        $this->_view->render();
    }

}